package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController//请求处理类
public class DeptController {
//   private static final Logger log= LoggerFactory.getLogger(DeptController.class);//日志对象
    @Autowired
    private DeptService deptService;

//    @RequestMapping(value="/depts",method= RequestMethod.GET)
   @GetMapping
    public Result list(){
//        System.out.println("查询全部部门数据");
       log.info("查询全部部门数据");
       List<Dept> deptList=deptService.findAll();
        return Result.success(deptList);
    }

//    方式一:
//    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request){
//        String idStr=request.getParameter("id");
//        int id=Integer.parseInt(idStr);
//        System.out.println("根据ID删除部门:"+id);
//        return Result.success();
//   }

//    方式二:
//@DeleteMapping("/depts")//required默认为true
//public Result delete(@RequestParam(value="id",required = false)Integer deptId){
//    System.out.println("根据ID删除部门:"+deptId);
//    return Result.success();
//}


//    方式三:
@Log
@DeleteMapping
//省略@RequestParam注解
public Result delete(Integer id){
//    System.out.println("根据ID删除部门:"+id);
    log.info("根据ID删除部门:{}",id);
    deptService.deleteById(id);
    return Result.success();
}
@Log
@PostMapping
public Result add(@RequestBody Dept dept){
//    System.out.println("新增部门:"+dept);
    log.info("新增部门:{}",dept);
    deptService.add(dept);
    return Result.success();
}

//根据ID查询部门
//    @GetMapping("/depts/{id}")
//    public Result getInfo(@PathVariable("id") Integer deptId){
//        System.out.println("根据ID查询部门数据:"+deptId);
//        return Result.success();
//    }

    //根据ID查询部门
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
//        System.out.println("根据ID查询部门:"+id);
        log.info("根据ID查询部门:{}",id);
        Dept dept=deptService.getById(id);
        return Result.success(dept);
    }

//    修改部门
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
//        System.out.println("修改部门:"+dept);
        log.info("修改部门:{}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
